import 'dart:convert';

import 'package:bloctest/generated/assets.dart';
import 'package:bloctest/model/login_model.dart';
import 'package:bloctest/utils/Constants.dart';
import 'package:bloctest/utils/DialogHelper.dart';
import 'package:bloctest/utils/RouteUtils.dart';
import 'package:bloctest/utils/Routes.dart';
import 'package:bloctest/utils/sp_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PersonPage extends StatefulWidget {
  const PersonPage({super.key});

  @override
  State<PersonPage> createState() => _PersonPageState();
}

class _PersonPageState extends State<PersonPage> {
  LoginModel? loginModel;

  @override
  void initState() {
    // TODO: implement initState

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    String? str = SpUtils.getString(Constants().USERINFO) ?? "";
    if (str.isNotEmpty) {
      loginModel = LoginModel.fromJson(jsonDecode(str));
    }
    print("111");
    print(loginModel);
    return getContent(context, loginModel);
  }
}

Widget getContent(BuildContext context, LoginModel? loginModel) {
  return Expanded(
      child: Column(
    children: [
      Container(
        width: double.infinity,
        height: 250.h,
        color: Colors.teal,
        child: Expanded(
          child: loginArea(context, loginModel),
        ),
      ),
      settingArea("我的收藏"),
      settingArea("检查更新"),
      settingArea("关于我们"),
      if (loginModel?.nickname != null)
        settingArea("退出登录", callback: () {
          DialogHelper.showConfirmDialog(context: context,title: "提示",content: "是否确认退出",confirmCall: (){
            SpUtils.clear();
            RouteUtils.pushNamedAndRemoveUntil(context, RouterPath.login);
          });

        }),
    ],
  ));
}

///登陆区域
Widget loginArea(BuildContext context, LoginModel? loginModel) {
  return GestureDetector(
    onTap: () {
      if (loginModel?.nickname == null) {
        RouteUtils.pushForName(context, RouterPath.login);
      }
    },
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        CircleAvatar(
          radius: 40.r,
          backgroundImage: getImageWidget(loginModel?.icon ?? ""),
        ),
        Padding(
          padding: EdgeInsets.only(top: 10.h),
          child: Text(
            loginModel?.nickname ?? "未登录",
            style: TextStyle(color: Colors.white, fontSize: 16.sp),
          ),
        )
      ],
    ),
  );
}

ImageProvider getImageWidget(String url) {
  if (url.isEmpty) {
    return const AssetImage(Assets.imagesIconEmptyHead);
  } else {
    return NetworkImage(url);
  }
}

Widget settingArea(str, {Function? callback}) {
  return InkWell(
    onTap: () {
      if (callback != null) {
        callback();
      }
    },
    child: Container(
      height: 50.h,
      padding: EdgeInsets.only(left: 10.r, right: 10.r),
      margin: EdgeInsets.only(left: 10.r, top: 10.r, right: 10.r),
      decoration: BoxDecoration(
          border: Border.all(color: Colors.grey, width: 1.r),
          borderRadius: BorderRadius.all(Radius.circular(10.r))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            str,
            style: TextStyle(fontSize: 16.sp, color: Colors.black),
          ),
          Image.asset(
            Assets.imagesIconMore,
            width: 20.w,
            height: 20.h,
          )
        ],
      ),
    ),
  );
}
